import { IPictureModel, PictureModel } from "@/Model/PictureModel";

export class PicturePriview {
  public Preview(files: FileList): Promise<IPictureModel> {
    return new Promise<IPictureModel>((resolve, reject) => {
      // 判断有文件
      if (files.length === 0) {
        return;
      }

      const file = files[0];
      // 判断文件是图片
      // 在浏览器中图片文件的类型都是 image/xxx
      if (file.type.match(/image\/*/) === null) {
        reject("选择的文件不是图片类型的！");
        return;
      }

      const pictrueModel: IPictureModel = new PictureModel();
      pictrueModel.Name = file.name;
      const reader = new FileReader();
      reader.onload = () => {
        if (typeof reader.result === "string") {
          pictrueModel.Image = reader.result;
          resolve(pictrueModel);
        }
      };
      reader.readAsDataURL(file);
    });
  }
}
